Ext.namespace("Power");
Power.List = function() {
		this.owner_id1=new Ext.form.Hidden({});
		this.site_id=new Ext.form.Hidden({});
		this.power_person=new Ext.form.Hidden({});
		
		this.owner_name1 = new Ext.form.TextField({
			fieldLabel: '姓名',
			blankText: '请填写联系人名称',
			maxLength:10
		});
		this.tele11 = new Ext.form.NumberField({
			fieldLabel: '手机1',
			blankText: '请填写手机1',
			maxLength:11
		});
		this.tele21 = new Ext.form.NumberField({
			fieldLabel: '手机2',
			blankText: '请填写手机2',
			maxLength:11
		});
		this.phone1 = new Ext.form.NumberField({
			fieldLabel: '电话',
			blankText: '请填写电话',
			maxLength:16
		});
		this.note1 = new Ext.form.TextArea({
			fieldLabel: '备注',
			blankText: '请填写备注',
			width:400,
			maxLength:100
		});
	    
	    this.btnSearch1 = new Ext.Toolbar.Button({
	        iconCls: 'icon-add',
	        text: '新增供电联系人',
	        tooltip: {text: '新增供电联系人'},
	        handler: this.initAdd1,
	        scope: this
	    });
	    this.btnDel1 = new Ext.Toolbar.Button({
	        iconCls: 'icon-delete',
	        text: '删除',
	        tooltip: {text: '删除联系人'},
	        handler: this.deleteOwner,
	        scope: this
	    });
	    this.btnAdd1= new Ext.Toolbar.Button({
	        iconCls: 'icon-add',
	        text: '保存',
	        tooltip: {text: '保存联系人'},
	        handler: this.addOwner,
	        scope: this
	    });
		
		
		this.formFieldSet1 = new Ext.form.FieldSet({
	        title: '联系人信息',
	        layout:'table',
	        autoHeight: true,
	        labelAlign:'right',
	        layoutConfig:{columns:2},
	        items:[
				{layout:'form',items:[this.owner_name1]},
				{layout:'form',items:[this.tele11]},
				{layout:'form',items:[this.tele21]},
				{layout:'form',items:[this.phone1]},
				{colspan:2,layout:'form',items:[this.note1]}
			]
		});   
		this.form1 = new Ext.form.FormPanel({
			bodyStyle : 'padding: 5px 5px 0',
			frame :true,
			//autoHeight : true,
			height:195,
			layout : 'table',
			align:'center',
			autoWidth:true,
			labelAlign: 'right',
			labelWidth : 150,
			layoutConfig : {columns:1},
			items:[
				this.formFieldSet1
			]
		});
		
		this.cm1 = new Ext.grid.ColumnModel({
		    defaults: {
		        width: 100,
		        sortable: true,
		        align: 'center'
		    },
		    columns: [
		    		{id: 'OWNER_ID', header: 'ID', width: 50, hidden: true, dataIndex: 'OWNER_ID'},
					{header: '供电联系人', width: 200, dataIndex: 'OWNER_NAME'},
					{header: '手机联系1', width: 100, dataIndex: 'TELE1'},
					{header: '手机联系2', width: 100, dataIndex: 'TELE2'},
					{header: '电话', dataIndex: 'PHONE'},
					{header: '备注', dataIndex: 'NOTE'}
		    ]
		});		
			
		
		this.store1 = new Ext.data.JsonStore({
		  	url: '../site/searchOwner.form?timesnap=' + Date.parse(new Date()),
			method: 'POST',
		    root: 'result',
		    fields: ['OWNER_ID', 'SITE_ID', 'OWNER_NAME', 'OWNER_DEP', 'TELE1', 'TELE2', 'PHONE', 'NOTE', 'POWER_PERSON'],
		    sortInfo: {
	            field: 'OWNER_ID',
	            direction: 'ASC'
	        }
		});
		
		this.grid1 = new Ext.grid.GridPanel({
			//title:'站点列表',
			height: 200,	
			width:650,
			store:this.store1,
			cm:this.cm1,
			displayInfo: true,			
			sm:new Ext.grid.RowSelectionModel({singleSelect:true}),
			stripeRows: true, 
	        loadMask: {msg: '加载中，请稍候...'},
	        tbar:[this.btnSearch1],
	        viewConfig: { forceFit: true },
	        listeners:{
	      	    rowclick : function( grid, rowIndex,e){
	      	    	var ownerId=grid.store.getAt(rowIndex).data.OWNER_ID;
	      	    	var siteId=grid.store.getAt(rowIndex).data.SITE_ID;
		      	    this.initEdit(ownerId,siteId);
				},
				scope:this				
	       }
		});
		
		Power.List.superclass.constructor.call(this,{
			title : '供电信息',
			frame:true,
	        resizable: false,
	        autoWidth:true,
	        modal: true,
	       	layout:'form',
	       	height:450,
	       	labelAlign:'right',
	        items: [this.grid1,this.form1],
	        buttons:[this.btnAdd1,this.btnDel1],
	        buttonAlign:'center'
		});	
};

Ext.extend(Power.List, Ext.Panel, {
	initAdd1:function(){
		this.owner_id1.setValue("");
		this.owner_name1.setValue("");
		this.tele11.setValue("");
		this.tele21.setValue("");
		this.phone1.setValue("");
		this.note1.setValue("");
	},
	initEdit:function(ownerId,siteId){
		if(ownerId){
			var conn = new Ext.data.Connection({
				url : '../site/searchOwnerById.form?timesnap=' + Date.parse(new Date()),
				method : 'POST'
			});
			Utils.request(conn, {
				params : {
					owner_id : ownerId
				},
				scope:this,
				success : function(data) {
						this.scope.owner_id1.setValue(data.result.OWNER_ID);
						this.scope.site_id.setValue(data.result.SITE_ID);
						this.scope.owner_name1.setValue(data.result.OWNER_NAME);
						this.scope.tele11.setValue(data.result.TELE1);
						this.scope.tele21.setValue(data.result.TELE2);
						this.scope.phone1.setValue(data.result.PHONE);
						this.scope.note1.setValue(data.result.NOTE);
						this.scope.power_person.setValue(1);
				},
				failure:function(){
					Utils.info('操作失败!','系统提示');
				}
			});
		}
	},
	addOwner :function(){
		if(!this.owner_name1.getValue()){
			Utils.info("姓名不能为空!","系统提示");
			return;
		}
		if(!this.form1.getForm().isValid()){
			Utils.info("信息填写不完整或有误,请检查!","系统提示");
			return;
		}
		var owner_id1= this.owner_id1.getValue();
		var site_id = this.site_id.getValue();
		var power_person=this.power_person.getValue();	
		//供电联系人
		var owner_name1 = this.owner_name1.getValue();
		var tele11 = this.tele11.getValue();
		var tele21 = this.tele21.getValue();
		var	phone1 = this.phone1.getValue();
		var	note1 = this.note1.getValue();
		var conn = new Ext.data.Connection({
				url : '../site/editOwner.form?timesnap=' + Date.parse(new Date()),
				method : 'POST'
			});
		//修改
		if(owner_id1){
			Utils.request(conn, {
					params : {
						owner_id : owner_id1,
						site_id : site_id,
						owner_name : owner_name1,
						tele1 : tele11,
						tele2 : tele21,
						phone : phone1,
						note : note1,
						power_person : 1
					},
					scope : this,
					success : function(resp) {
						this.scope.initAdd1();
						Power.List.instance .store1.reload();
					},
					failure:function(resp){
						Utils.info('操作失败','系统提醒');
					}
			});
		}
		//新增
		else{
			Utils.request(conn, {
					params : {
						site_id : site_id,
						owner_name : owner_name1,
						tele1 : tele11,
						tele2 : tele21,
						phone : phone1,
						note : note1,
						power_person : 1
					},
					scope : this,
					success : function(resp) {
						this.scope.initAdd1();
						Power.List.instance.store1.reload();
					},
					failure:function(resp){
						Utils.info('操作失败','系统提醒');
					}
				});
		}
	},
	searchOwner:function(data){
		this.site_id.setValue(data);
		this.power_person.setValue(1);
		this.store1.load({params: {
				site_id:this.site_id.getValue(),
				power_person: 1
		}});		
	},
	deleteOwner:function(){
		var record = this.grid1.getSelectionModel().getSelected();
		if(record===null){
			Utils.warn('请选择一条记录！','警告');
			return ;
		}
		var conn = new Ext.data.Connection({
				url : '../site/deleteOwner.form?timesnap=' + Date.parse(new Date()),
				method : 'POST'
			});
		Utils.request(conn, {
					params : {
						owner_id : record.data.OWNER_ID
					},
					scope : this,
					success : function(resp) {
						this.scope.initAdd1();
						Power.List.instance.store1.reload();
					},
					failure:function(resp){
						Utils.info('操作失败','系统提醒');
					}
		});
	}
});